package com.zcx.qshop.adapter;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.entity.Good;

import java.util.List;

/**
 * Created by Administrator on 2015/9/4 0004.
 */
public class SearchResultAdapter extends QSAdapter<Good> {

    private LayoutInflater layoutInflater;

    public SearchResultAdapter(Context context, List<Good> objects) {

        super(context, objects);

        layoutInflater = ((Activity)context).getLayoutInflater();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Good good = getItem(position);

        ViewHolder viewHolder;

        if(convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), QSApplication.ScaleScreenHelper.loadView((ViewGroup) layoutInflater.inflate(R.layout.item_search_result, null)));

            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolder) convertView.getTag();

        }

        viewHolder.img.setImageURI(Uri.parse(good.picurl));

        viewHolder.name.setText(Html.fromHtml(good.title));

        try{

            viewHolder.context.setText(Html.fromHtml(good.description));

        }catch (Exception e){}

        viewHolder.price.setText("￥" + good.price + "元");

        viewHolder.unit.setText("/" + good.unit);

        viewHolder.count.setText("销售 " + good.number);

        return convertView;

    }

    public static class ViewHolder{

        @BoundView(R.id.item_search_result_img) private SimpleDraweeView img;

        @BoundView(R.id.item_search_result_name) private TextView name;

        @BoundView(R.id.item_search_result_context) private TextView context;

        @BoundView(R.id.item_search_result_price) private TextView price;

        @BoundView(R.id.item_search_result_unit) private TextView unit;

        @BoundView(R.id.item_search_result_count) private TextView count;

    }

}
